<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="userNikeName">
      <el-input v-model="dataForm.userNikeName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="userPwd">
      <el-input v-model="dataForm.userPwd" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="真实姓名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="电子邮箱" prop="userEmail">
      <el-input v-model="dataForm.userEmail" placeholder="电子邮箱"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="userTel">
      <el-input v-model="dataForm.userTel" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="userMaibox">
      <el-input v-model="dataForm.userMaibox" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="冻结状态 1活跃 2注销" prop="userStatus">
      <el-input v-model="dataForm.userStatus" placeholder="冻结状态 1活跃 2注销"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userNikeName: '',
          userPwd: '',
          userName: '',
          userEmail: '',
          userTel: '',
          userMaibox: '',
          userStatus: ''
        },
        dataRule: {
          userNikeName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          userPwd: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          userEmail: [
            { required: true, message: '电子邮箱不能为空', trigger: 'blur' }
          ],
          userTel: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          userMaibox: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          userStatus: [
            { required: true, message: '冻结状态 1活跃 2注销不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userNikeName = data.user.userNikeName
                this.dataForm.userPwd = data.user.userPwd
                this.dataForm.userName = data.user.userName
                this.dataForm.userEmail = data.user.userEmail
                this.dataForm.userTel = data.user.userTel
                this.dataForm.userMaibox = data.user.userMaibox
                this.dataForm.userStatus = data.user.userStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userNikeName': this.dataForm.userNikeName,
                'userPwd': this.dataForm.userPwd,
                'userName': this.dataForm.userName,
                'userEmail': this.dataForm.userEmail,
                'userTel': this.dataForm.userTel,
                'userMaibox': this.dataForm.userMaibox,
                'userStatus': this.dataForm.userStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
